package com.example.anni.zhuzhoujia.tianya12345;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.anni.zhuzhoujia.R;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yangshengwen2016 on 2016/6/13.
 * 我参与的活动
 */
public class CityFragmentMyParticipateIn extends Fragment {
    public GridView list_item;
    public List<Map<String, Object>> list;
    public JSONArray data;
    public JSONArray participateIn;//我参与的活动

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.city_grid_view, null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //调用登录接口
        RequestParams params1 = new RequestParams("http://tr.zzapi.gson.cn/api/user/login");
        params1.addBodyParameter("userName", "测试");
        params1.addBodyParameter("password", "666666");
        x.http().post(params1, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {

            }

            @Override
            public void onError(Throwable throwable, boolean b) {

            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {

            }
        });
        list_item = (GridView) view.findViewById(R.id.gv);
        //获取我发起的活动列表
        RequestParams params = new RequestParams("http://tr.zzapi.gson.cn/api/user/activity");
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(final String s) {
                com.alibaba.fastjson.JSONObject json = JSON.parseObject(s);
                data = json.getJSONArray("data");
                //创建适配器
                class DiscloseAdapter extends BaseAdapter {
                    public DiscloseAdapter() {
                        list = new ArrayList<>();
                        for (int i = 0; i < data.size(); i++) {
                            JSONObject obj1 = data.getJSONObject(i);
                            participateIn = obj1.getJSONArray("participateIn");//我发起的活动
                            for (int j = 0; j < participateIn.size(); j++) {
                                JSONObject obj2 = participateIn.getJSONObject(j);
                                String activity_id = obj2.getString("id");//id
                                String title = obj2.getString("title");//标题
                                String startLine = obj2.getString("startLine");//开始时间
                                String endLine = obj2.getString("endLine");//结束时间
                                String dateline = obj2.getString("dateline");//活动创建时间
                                String address = obj2.getString("address");//地址
                                String area = obj2.getString("area");//活动区域
                                if (area.equals("SF")) {
                                    area = "石峰区";
                                }
                                if (area.equals("YL")) {
                                    area = "炎陵县";
                                }
                                if (area.equals("YLSFFQ")) {
                                    area = "云龙示范区";
                                }
                                if (area.equals("ZZX")) {
                                    area = "株洲县";
                                }
                                if (area.equals("YX")) {
                                    area = "攸县";
                                }
                                if (area.equals("TY")) {
                                    area = "天元区";
                                }
                                if (area.equals("LL")) {
                                    area = "醴陵市";
                                }
                                if (area.equals("LS")) {
                                    area = "芦淞区";
                                }
                                if (area.equals("HT")) {
                                    area = "荷塘区";
                                }
                                if (area.equals("CL")) {
                                    area = "茶陵县";
                                }
                                String participateIns = obj2.getString("participateIns");//已参与人数
                                Map<String, Object> map = new HashMap<>();
                                map.put("title", title);
                                map.put("activity_id", activity_id);
                                map.put("startLine", startLine);
                                map.put("endLine", endLine);
                                map.put("dateline", dateline);
                                map.put("address", address);
                                map.put("area", area);
                                map.put("participateIns", participateIns);
                                list.add(map);
                            }
                        }
                    }

                    public int getCount() {
                        return list.size();
                    }

                    @Override
                    public Object getItem(int position) {
                        return list.get(position);
                    }

                    @Override
                    public long getItemId(int position) {
                        return position;
                    }

                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {
                        final LayoutTextView5 layoutTextView5;
                        if (convertView == null) {
                            convertView = LayoutInflater.from(getActivity()).inflate(R.layout.city_fragment_my_publish, null);
                            layoutTextView5 = new LayoutTextView5();
                            layoutTextView5.tv_title = (TextView) convertView.findViewById(R.id.title);
                            layoutTextView5.tv_startLine = (TextView) convertView.findViewById(R.id.startLine);
                            layoutTextView5.tv_endLine = (TextView) convertView.findViewById(R.id.endLine);
                            layoutTextView5.tv_address = (TextView) convertView.findViewById(R.id.address);
                            layoutTextView5.tv_dateline = (TextView) convertView.findViewById(R.id.dateline);
                            layoutTextView5.tv_area = (TextView) convertView.findViewById(R.id.area);
                            layoutTextView5.tv_participateIns = (TextView) convertView.findViewById(R.id.participateIns);
                            layoutTextView5.cv = (CardView) convertView.findViewById(R.id.cv);
                            convertView.setTag(layoutTextView5);
                        } else {
                            layoutTextView5 = (LayoutTextView5) convertView.getTag();
                        }

                        String startLine = list.get(position).get("startLine") + "";//开始时间
                        String endLine = list.get(position).get("endLine") + "";//结束时间
                        layoutTextView5.tv_title.setText((String) list.get(position).get("title"));
                        layoutTextView5.tv_startLine.setText(startLine.substring(0, 10));
                        layoutTextView5.tv_endLine.setText(endLine.substring(0, 10));
                        layoutTextView5.tv_dateline.setText("" + list.get(position).get("dateline"));
                        layoutTextView5.tv_address.setText("" + list.get(position).get("address"));
                        layoutTextView5.tv_participateIns.setText("" + list.get(position).get("participateIns"));
                        layoutTextView5.tv_area.setText("" + list.get(position).get("area"));
                        layoutTextView5.cv.setBackgroundColor(0x220099FF);
                        //获得系统时间
                        Calendar c = Calendar.getInstance();
                        int year = c.get(Calendar.YEAR);//年
                        int month = c.get(Calendar.MONTH) + 1;//月
                        int day = c.get(Calendar.DAY_OF_MONTH);//日
                        int hour = c.get(Calendar.HOUR_OF_DAY);//时
                        int minute = c.get(Calendar.MINUTE);//分
                        String months = month + "";
                        String days = day + "";
                        if (months.length() == 1) {
                            months = "0" + month;
                        }
                        if (days.length() == 1) {
                            days = "0" + day;
                        }
                        String time = year + "-" + months + "-" + days;
                        String date = "" + list.get(position).get("dateline");//服务器上的日期
                        //如果获得的日期与系统相等则显示今天
                        if (time.equals(date.substring(0, 10))) {
                            layoutTextView5.tv_dateline.setText("今天" + date.substring(10));
                        } else {
                            layoutTextView5.tv_dateline.setText("" + list.get(position).get("dateline"));
                        }
                        return convertView;
                    }
                }
                //添加适配器
                BaseAdapter a = new DiscloseAdapter();
                a.notifyDataSetChanged();
                list_item.setAdapter(a);
                //查看活动详情
                list_item.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        BaseAdapter hvla = (BaseAdapter) parent.getAdapter();
                        //从列表项获取数据
                        Map<String, String> m = (Map<String, String>) hvla.getItem(position);
                        Intent intent = new Intent(getActivity(), CityDetails.class);
                        intent.putExtra("id", m.get("activity_id"));
                        startActivity(intent);
                    }
                });
            }

            class LayoutTextView5 {
                public TextView tv_title;
                public TextView tv_startLine;
                public TextView tv_endLine;
                public TextView tv_address;
                public TextView tv_dateline;
                public TextView tv_area;
                public TextView tv_participateIns;
                public CardView cv;
            }
            @Override
            public void onError(Throwable throwable, boolean b) {

            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}